package com.zb.knowledge.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.knowledge.service.ContInstructionBookService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjInstructionBookService;
import com.zb.utils.TokenUtil;
import com.zb.vo.InstructionBookSelectVo;
import com.zb.vo.InstructionBookVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 产品说明书表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Api(tags = "知识中心说明书类接口")
@RestController
@RequestMapping("/knowledge/instruction-book")
public class ZjInstructionBookController {
    @Resource
    private ZjInstructionBookService instructionBookService;
    @Resource
    private ContInstructionBookService contInstructionBookService;

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "知识中心-说明书 - 新增或修改说明书")
    @ApiOperation("新增或修改说明书")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid InstructionBookVo instructionBookVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        instructionBookService.save(custAccId, instructionBookVo);
        return Result.success(null == instructionBookVo.getId()? "新增说明书成功" : "修改说明书成功");
    }


    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.SELECT, remark = "知识中心-说明书 - 获取说明书列表")
    @ApiOperation("获取说明书列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody InstructionBookSelectVo bookSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取说明书列表成功").data(instructionBookService.getList(custAccId, bookSelectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.SELECT, remark = "知识中心-说明书 - 获取说明书信息")
    @ApiOperation("获取说明书信息")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "说明书ID", required = true) Long id) {
        InstructionBookVo instructionBookVo = instructionBookService.getInfo(id);
        return Result.success("获取说明书信息成功").data("row", instructionBookVo);
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "知识中心-说明书 - 批量启用或禁用说明书")
    @ApiOperation("批量启用或禁用说明书")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "说明书ID集合", required = true) List<Long> idList) {
        instructionBookService.updateBatchDisableByIds(idList, status);
        return Result.success(status? "禁用说明书成功" : "启用说明书成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.KNOWLEDGE_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "知识中心-说明书 - 批量删除说明书")
    @ApiOperation("批量删除说明书")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "说明书ID集合", required = true) List<Long> idList) {
        contInstructionBookService.deleteByIds(idList);
        return Result.success("删除说明书成功");
    }

    @ApiOperation("根据知识库分类ID获取说明书")
    @GetMapping("/getInstructionBook/{knowledgeTypeId}")
    public Result getInstructionBook(@PathVariable @ApiParam(value = "知识库分类表ID") Long knowledgeTypeId) {
        return Result.success().data(PageUtil.row(contInstructionBookService.getInstructionBook(knowledgeTypeId)));
    }

}
